/**
 * 
 */
package org.lightmtv.config;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.lightmtv.request.Request;


/**
 * GlobalContext is use to hold settings and ThreadLocal variables;
 * it is just an holder,and it will do nothing
 * @author gl
 * @since Jul 2, 2008
 */
public class GlobalContext {
	
	private static Settings settings;
	private static Map vars = new HashMap();

	private static ThreadLocal<Request> _request = new ThreadLocal<Request>();
	private static ThreadLocal<Locale> _locale = new ThreadLocal<Locale>(){
		@Override
		protected Locale initialValue() {
			return Locale.getDefault();
		}
	};
	
	/**
	 * @return the settings
	 */
	public static Settings getSettings() {
		return settings;
	}
	
	/**
	 * @param settings the settings to set
	 */
	public static void setSettings(Settings settings) {
		GlobalContext.settings = settings;
	}

	public static HttpSession getSession(){
		return getRequest().getSession();
	}
	
	public static Request getRequest(){
		return (Request) _request.get();
	}
	
	public static void setRequest(Request request){
		_request.set(request);
	}

	public static Locale getLocale(){
		return _locale.get();
	}
	
	public static void setLocale(Locale locale){
		_locale.set(locale);
	}
	
	/**
	 * put something to GlobalContext
	 * @param key
	 * @param value
	 */
	public static void put(String key,Object value){
		vars.put(key, value);
	}

	/**
	 * get something from GlobalContext
	 * @param key
	 * @return
	 */
	public static Object get(String key){
		return vars.get(key);
	}
	
	/**
	 * 
	 * @param key
	 * @return
	 */
	public static Object remove(String key){
		return vars.remove(key);
	}
	
	/**
	 * 
	 * @return the GlobalContext vars map, use ${global} in template
	 */
	public static Map getVars() {
		return vars;
	}
	
}
